package com.vitabuybuy.tv.utils

import android.os.Build
import android.text.TextUtils
import android.webkit.WebSettings
import android.webkit.WebView
import kotlinx.android.synthetic.main.activity_bidding_commodity.*

/**
 * 作者：SimGa
 * 日期：2020/12/26
 * 时间：15:38
 * 包名：com.vitabuybuy.tv.utils
 */
class WebViewUtils {
    companion object {
        fun defaultSetting(webView: WebView) {
            val settings = webView.settings
            //设置WebView属性，能够执行Javascript脚本
            settings.javaScriptCanOpenWindowsAutomatically = true
            settings.javaScriptEnabled = true//启用js
//        settings.useWideViewPort = true
//        settings.loadWithOverviewMode = true
            settings.defaultTextEncodingName = "utf-8"
            settings.allowFileAccess = true
            settings.domStorageEnabled = true
            settings.setSupportMultipleWindows(true)
            settings.blockNetworkImage = false//解决图片不显示
            settings.setSupportZoom(false)
            settings.builtInZoomControls = false
            settings.allowFileAccess = true
            settings.databaseEnabled = true
            settings.setGeolocationEnabled(true)
            settings.setAppCacheEnabled(true)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                settings.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
            }
        }

        fun htmlContent(oldContent: String?): String {
            var newContent = ""
            if (!TextUtils.isEmpty(oldContent)) {
                val head = "<head>" +
                        "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> " + "<style>img{max-width: 100%; width:auto; height:auto;} body{word-break:break-all;}</style>" + "</head>"
                newContent = "<html>$head${oldContent}</html>"
            }
            return newContent;
        }
    }
}